package com.macro.mall.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.portal.service.SmsFlashPromotionService;
import com.macro.mall.portal.service.UmsMemberService;
import com.macro.mall.portal.vo.SmsFlashPromotionProductRelationVO;
import com.macro.mall.portal.vo.SmsFlashPromotionSessionVO;
import com.macro.mall.portal.vo.SmsFlashPromotionVO;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SmsFlashPromotionServiceImpl implements SmsFlashPromotionService {
    @Autowired
    private SmsFlashPromotionMapper smsFlashPromotionMapper;
    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;
    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;
    @Autowired
    private SmsFlashPromotionProductMemberRelationMapper smsFlashPromotionProductMemberRelationMapper;
    @Autowired
    private PmsProductMapper pmsProductMapper;
    @Autowired
    private UmsMemberService umsMemberService;
    @Override
    public List<SmsFlashPromotion> list() {
        SmsFlashPromotionExample example = new SmsFlashPromotionExample();
        example.createCriteria().andStatusEqualTo(1).andEndDateGreaterThan(new Date());
        return smsFlashPromotionMapper.selectByExample(example);
    }

    @Override
    public SmsFlashPromotionVO listGetById(Long flashPromotionId) {
        //限时购表
        SmsFlashPromotionVO smsFlashPromotionVO = new SmsFlashPromotionVO();
        SmsFlashPromotion smsFlashPromotion = smsFlashPromotionMapper.selectByPrimaryKey(flashPromotionId);
        BeanUtils.copyProperties(smsFlashPromotion,smsFlashPromotionVO);
        System.out.println("smsFlashPromotionVO="+smsFlashPromotionVO);
        //限时购场次表
        SmsFlashPromotionSessionExample smsFlashPromotionSessionExample = new SmsFlashPromotionSessionExample();
        smsFlashPromotionSessionExample.createCriteria().andStatusEqualTo(1);
        List<SmsFlashPromotionSession> smsFlashPromotionSessions = smsFlashPromotionSessionMapper.selectByExample(smsFlashPromotionSessionExample);
        System.out.println(smsFlashPromotionSessions);
        List<SmsFlashPromotionSessionVO> smsFlashPromotionSessionVOS = new ArrayList<>();
        for (SmsFlashPromotionSession sp: smsFlashPromotionSessions){
            SmsFlashPromotionSessionVO smsFlashPromotionSessionVO = new SmsFlashPromotionSessionVO();
            BeanUtils.copyProperties(sp,smsFlashPromotionSessionVO);
            System.out.println(smsFlashPromotionSessionVO);
            smsFlashPromotionSessionVOS.add(smsFlashPromotionSessionVO);
        }
        //商品限时购与商品关系表
        System.out.println("smsFlashPromotionSessionVOS="+smsFlashPromotionSessionVOS);
        UmsMember member = umsMemberService.getCurrentMember();
        for (SmsFlashPromotionSessionVO sfp : smsFlashPromotionSessionVOS){
            SmsFlashPromotionProductRelationExample smsFlashPromotionProductRelationExample = new SmsFlashPromotionProductRelationExample();
            smsFlashPromotionProductRelationExample.createCriteria().andFlashPromotionIdEqualTo(smsFlashPromotionVO.getId()).andFlashPromotionSessionIdEqualTo(sfp.getId());
            List<SmsFlashPromotionProductRelationVO> smsFlashPromotionProductRelationVOS = smsFlashPromotionProductRelationMapper.selectByExample(smsFlashPromotionProductRelationExample).stream().map(smsFlashPromotionProductRelation -> {
                SmsFlashPromotionProductRelationVO smsFlashPromotionProductRelationVO = new SmsFlashPromotionProductRelationVO();
                BeanUtils.copyProperties(smsFlashPromotionProductRelation,smsFlashPromotionProductRelationVO);
                smsFlashPromotionProductRelationVO.setPmsProduct(pmsProductMapper.selectByPrimaryKey(smsFlashPromotionProductRelationVO.getProductId()));
                if (member!=null){
                    SmsFlashPromotionProductMemberRelationExample sfppmr = new SmsFlashPromotionProductMemberRelationExample();
                    sfppmr.createCriteria().andMemberIdEqualTo(member.getId()).andPromotionProductRelationIdEqualTo(smsFlashPromotionProductRelationVO.getId());
                    List<SmsFlashPromotionProductMemberRelation> smsFlashPromotionProductMemberRelations = smsFlashPromotionProductMemberRelationMapper.selectByExample(sfppmr);
                    if (CollUtil.isNotEmpty(smsFlashPromotionProductMemberRelations)){
                        smsFlashPromotionProductRelationVO.setSmsFlashPromotionProductMemberRelation(smsFlashPromotionProductMemberRelations.get(0));
                    }
                }
                return smsFlashPromotionProductRelationVO;
            }).collect(Collectors.toList());
            List<SmsFlashPromotionProductRelationVO> smsFlashPromotionProductRelationVOS1 = new ArrayList<>();
            smsFlashPromotionProductRelationVOS.stream().sorted(Comparator.comparing(SmsFlashPromotionProductRelationVO::getSort).reversed()).forEach(
                    s->{
                        SmsFlashPromotionProductRelationVO smsFlashPromotionProductRelationVO = new SmsFlashPromotionProductRelationVO();
                        BeanUtils.copyProperties(s,smsFlashPromotionProductRelationVO);
                        smsFlashPromotionProductRelationVOS1.add(smsFlashPromotionProductRelationVO);
                    }

            );
            sfp.setSmsFlashPromotionProductRelationVOS(smsFlashPromotionProductRelationVOS1);
        }
        smsFlashPromotionVO.setSmsFlashPromotionSessionVOS(smsFlashPromotionSessionVOS);
        System.out.println(smsFlashPromotionVO);
        return smsFlashPromotionVO;
    }

    @Override
    public SmsFlashPromotionVO flashPromotion() throws ParseException {
        SmsFlashPromotionExample flashPromotionExample = new SmsFlashPromotionExample();
        flashPromotionExample.createCriteria().andStatusEqualTo(1).andEndDateGreaterThan(new Date());
        List<SmsFlashPromotion> smsFlashPromotions = smsFlashPromotionMapper.selectByExample(flashPromotionExample);
        if (CollUtil.isNotEmpty(smsFlashPromotions)){
            if (smsFlashPromotions.size()==1){
                    //限时购表
                    SmsFlashPromotionVO smsFlashPromotionVO = new SmsFlashPromotionVO();
                    SmsFlashPromotion smsFlashPromotion = smsFlashPromotionMapper.selectByPrimaryKey(smsFlashPromotions.get(0).getId());
                    BeanUtils.copyProperties(smsFlashPromotion,smsFlashPromotionVO);
                    //限时购场次表
                    SmsFlashPromotionSessionExample smsFlashPromotionSessionExample = new SmsFlashPromotionSessionExample();
                    smsFlashPromotionSessionExample.createCriteria().andStatusEqualTo(1);
                    List<SmsFlashPromotionSession> smsFlashPromotionSessions = smsFlashPromotionSessionMapper.selectByExample(smsFlashPromotionSessionExample);
                    System.out.println(smsFlashPromotionSessions);
                    List<SmsFlashPromotionSessionVO> smsFlashPromotionSessionVOS = new ArrayList<>();
                    SimpleDateFormat sdf =   new SimpleDateFormat( "HH:mm:ss" );
                    Date date= new Date();
                    for (SmsFlashPromotionSession sp: smsFlashPromotionSessions){
                        SmsFlashPromotionSessionVO smsFlashPromotionSessionVO = new SmsFlashPromotionSessionVO();
                       if (sdf.parse(sdf.format(date)).after(sdf.parse(sdf.format(sp.getStartTime()))) && sdf.parse(sdf.format(date)).before(sdf.parse(sdf.format(sp.getEndTime()))) ){
                           BeanUtils.copyProperties(sp,smsFlashPromotionSessionVO);
                           smsFlashPromotionSessionVOS.add(smsFlashPromotionSessionVO);
                        }

                    }

                    //商品限时购与商品关系表
                    UmsMember member = umsMemberService.getCurrentMember();
                    if (CollUtil.isNotEmpty(smsFlashPromotionSessionVOS)){
                        for (SmsFlashPromotionSessionVO sfp : smsFlashPromotionSessionVOS){
                            System.out.println("sfp===="+sfp);
                            SmsFlashPromotionProductRelationExample smsFlashPromotionProductRelationExample = new SmsFlashPromotionProductRelationExample();
                            smsFlashPromotionProductRelationExample.createCriteria().andFlashPromotionIdEqualTo(smsFlashPromotionVO.getId()).andFlashPromotionSessionIdEqualTo(sfp.getId());
                            List<SmsFlashPromotionProductRelationVO> smsFlashPromotionProductRelationVOS = smsFlashPromotionProductRelationMapper.selectByExample(smsFlashPromotionProductRelationExample).stream().map(smsFlashPromotionProductRelation -> {
                                SmsFlashPromotionProductRelationVO smsFlashPromotionProductRelationVO = new SmsFlashPromotionProductRelationVO();
                                BeanUtils.copyProperties(smsFlashPromotionProductRelation,smsFlashPromotionProductRelationVO);
                                smsFlashPromotionProductRelationVO.setPmsProduct(pmsProductMapper.selectByPrimaryKey(smsFlashPromotionProductRelationVO.getProductId()));
                                if (member!=null){
                                    SmsFlashPromotionProductMemberRelationExample sfppmr = new SmsFlashPromotionProductMemberRelationExample();
                                    sfppmr.createCriteria().andMemberIdEqualTo(member.getId()).andPromotionProductRelationIdEqualTo(smsFlashPromotionProductRelationVO.getId());
                                    List<SmsFlashPromotionProductMemberRelation> smsFlashPromotionProductMemberRelations = smsFlashPromotionProductMemberRelationMapper.selectByExample(sfppmr);
                                    if (CollUtil.isNotEmpty(smsFlashPromotionProductMemberRelations)){
                                        smsFlashPromotionProductRelationVO.setSmsFlashPromotionProductMemberRelation(smsFlashPromotionProductMemberRelations.get(0));
                                    }
                                }
                                return smsFlashPromotionProductRelationVO;
                            }).collect(Collectors.toList());
                            List<SmsFlashPromotionProductRelationVO> smsFlashPromotionProductRelationVOS1 = new ArrayList<>();
                            smsFlashPromotionProductRelationVOS.stream().sorted(Comparator.comparing(SmsFlashPromotionProductRelationVO::getSort).reversed()).forEach(
                                    s->{
                                        SmsFlashPromotionProductRelationVO smsFlashPromotionProductRelationVO = new SmsFlashPromotionProductRelationVO();
                                        BeanUtils.copyProperties(s,smsFlashPromotionProductRelationVO);
                                        smsFlashPromotionProductRelationVOS1.add(smsFlashPromotionProductRelationVO);
                                    }

                            );
                            sfp.setSmsFlashPromotionProductRelationVOS(smsFlashPromotionProductRelationVOS1);
                        }
                    }
                    smsFlashPromotionVO.setSmsFlashPromotionSessionVOS(smsFlashPromotionSessionVOS);
                    System.out.println(smsFlashPromotionVO);
                    return smsFlashPromotionVO;
            }else{
                    Random random = new Random();
                    int n = random.nextInt(smsFlashPromotions.size());
                    SmsFlashPromotion flashPromotion = smsFlashPromotions.get(n);
                    //限时购表
                    SmsFlashPromotionVO smsFlashPromotionVO = new SmsFlashPromotionVO();
                    SmsFlashPromotion smsFlashPromotion = smsFlashPromotionMapper.selectByPrimaryKey(flashPromotion.getId());
                    BeanUtils.copyProperties(smsFlashPromotion,smsFlashPromotionVO);
                    //限时购场次表
                    SmsFlashPromotionSessionExample smsFlashPromotionSessionExample = new SmsFlashPromotionSessionExample();
                    smsFlashPromotionSessionExample.createCriteria().andStatusEqualTo(1);
                    List<SmsFlashPromotionSession> smsFlashPromotionSessions = smsFlashPromotionSessionMapper.selectByExample(smsFlashPromotionSessionExample);
                    System.out.println(smsFlashPromotionSessions);
                    List<SmsFlashPromotionSessionVO> smsFlashPromotionSessionVOS = new ArrayList<>();
                    SimpleDateFormat sdf =   new SimpleDateFormat( "HH:mm:ss" );
                    Date date= new Date();
                    for (SmsFlashPromotionSession sp: smsFlashPromotionSessions){
                        SmsFlashPromotionSessionVO smsFlashPromotionSessionVO = new SmsFlashPromotionSessionVO();
                        if (sdf.parse(sdf.format(date)).after(sdf.parse(sdf.format(sp.getStartTime()))) && sdf.parse(sdf.format(date)).before(sdf.parse(sdf.format(sp.getEndTime()))) ){
                            BeanUtils.copyProperties(sp,smsFlashPromotionSessionVO);
                            smsFlashPromotionSessionVOS.add(smsFlashPromotionSessionVO);
                        }
                    }
//                    if (CollUtil.isEmpty(smsFlashPromotionSessionVOS)){
//                        return null;
//                    }
                    //商品限时购与商品关系表
                    //UmsMember member = umsMemberService.getCurrentMember();
                    for (SmsFlashPromotionSessionVO sfp : smsFlashPromotionSessionVOS){
                        SmsFlashPromotionProductRelationExample smsFlashPromotionProductRelationExample = new SmsFlashPromotionProductRelationExample();
                        smsFlashPromotionProductRelationExample.createCriteria().andFlashPromotionIdEqualTo(smsFlashPromotionVO.getId()).andFlashPromotionSessionIdEqualTo(sfp.getId());
                        List<SmsFlashPromotionProductRelationVO> smsFlashPromotionProductRelationVOS = smsFlashPromotionProductRelationMapper.selectByExample(smsFlashPromotionProductRelationExample).stream().map(smsFlashPromotionProductRelation -> {
                            SmsFlashPromotionProductRelationVO smsFlashPromotionProductRelationVO = new SmsFlashPromotionProductRelationVO();
                            BeanUtils.copyProperties(smsFlashPromotionProductRelation,smsFlashPromotionProductRelationVO);
                            smsFlashPromotionProductRelationVO.setPmsProduct(pmsProductMapper.selectByPrimaryKey(smsFlashPromotionProductRelationVO.getProductId()));
//                            if (member!=null){
//                                SmsFlashPromotionProductMemberRelationExample sfppmr = new SmsFlashPromotionProductMemberRelationExample();
//                                sfppmr.createCriteria().andMemberIdEqualTo(member.getId()).andPromotionProductRelationIdEqualTo(smsFlashPromotionProductRelationVO.getId());
//                                List<SmsFlashPromotionProductMemberRelation> smsFlashPromotionProductMemberRelations = smsFlashPromotionProductMemberRelationMapper.selectByExample(sfppmr);
//                                if (CollUtil.isNotEmpty(smsFlashPromotionProductMemberRelations)){
//                                    smsFlashPromotionProductRelationVO.setSmsFlashPromotionProductMemberRelation(smsFlashPromotionProductMemberRelations.get(0));
//                                }
//                            }
                            return smsFlashPromotionProductRelationVO;
                        }).collect(Collectors.toList());
                        List<SmsFlashPromotionProductRelationVO> smsFlashPromotionProductRelationVOS1 = new ArrayList<>();
                        smsFlashPromotionProductRelationVOS.stream().sorted(Comparator.comparing(SmsFlashPromotionProductRelationVO::getSort).reversed()).forEach(
                                s->{
                                    SmsFlashPromotionProductRelationVO smsFlashPromotionProductRelationVO = new SmsFlashPromotionProductRelationVO();
                                    BeanUtils.copyProperties(s,smsFlashPromotionProductRelationVO);
                                    smsFlashPromotionProductRelationVOS1.add(smsFlashPromotionProductRelationVO);
                                }

                        );
                        sfp.setSmsFlashPromotionProductRelationVOS(smsFlashPromotionProductRelationVOS1);
                    }
                    smsFlashPromotionVO.setSmsFlashPromotionSessionVOS(smsFlashPromotionSessionVOS);
                    System.out.println(smsFlashPromotionVO);
                    return smsFlashPromotionVO;
            }
        }else{
            return null;
        }


    }
}
